// pages/land/land.js
import util from '../../utils/area.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    label:['全部','耕地','林地','园地','草地','养殖','其他'],
    type:['转包','出租','互换','抵押'],
    order:['升序','降序'],
    value1:0,
    current:0,
    value2:'a',
    tabSelect:false,
    areaList:util,
    province:'',
    urban:'',
    area:'地区',
    areaArray:'',     
    area:{
        province_list:{},
        city_list:{} ,
        county_list:{},
     },
    rightshow:false,
    rightsed1:-1,
    rightsed2:-1,
    landList:[],
    ani1:'',
    ani2:'',
    ani3:'',
    bottom:false
  },

  
  getData(num,page){
      var area = this.data.areaArray;
      var index1 = this.data.rightsed1;
      var index2 = this.data.current;
      var type = '';
      var label = '';
      if(index1 != -1){
        type = this.data.type[index1]
      }
      if(index2 != 0){
        label = this.data.label[index2];
      }
        var that = this;
        console.log(area)
        wx.cloud.callFunction({
            // 云函数名称
            name: 'getLand',
            // 传给云函数的参数
            data:{
                num:num,
                page:page,
                area:area,
                label:label,
                type:type
            }
          }).then(res => {
              var oldList = this.data.landList;
              var newList = oldList.concat(res.result.data);
              that.setData({
                  landList : newList
              })
            }).catch(console.error)
  },

  tabSelect:function(e){
      var current = e.currentTarget.dataset.id
      this.setData({
        current:current,
        landList:[]
      })
      this.getData(6,0);
  },

  onringhtselected1(e){
    this.setData({
        rightsed1:e.currentTarget.dataset.id,
        landList:[]
    })
    this.getData(6,0)
  },

  onringhtselected2(e){
    var index = e.currentTarget.dataset.id;
    const{landList} = this.data;
     landList.sort(this.compare("area",index))   

    this.setData({
        rightsed2:index,
        landList:landList
    })

  },
  
  onrightshow(){
    this.setData({
        rightshow:!this.data.rightshow
    })
  },
  tabshow(){  

       var that=this;
        that.setData({
          tabSelect:!this.data.tabSelect
        })
  },
  onConfirm(e){
    var str=e.detail.values;
    var s='';
    str.forEach(element => {
        s=s+element.name+'';
    });

    this.setData({
        tabSelect:false,
        landList:[],
        areaArray:s,     
    })
    this.getData(6,0)
},

    onCancel(){
        this.setData({
            tabSelect:false
        })
    },
    onLoad: function (options) {
        this.getData(6,0);
    },
    OnClear()
    {
        this.setData({
        value1:0,
        value2:'a'
        })
    },

  onReachBottom: function () {
    this.setData({
        bottom:true
    })  
    var ani=wx.createAnimation({
      delay: 0,
      duration:400, 
      timingFunction:'ease'
    })
    ani.opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(1).step();
    this.setData({
        ani1:ani.export()
    })
    ani=wx.createAnimation({
      delay: 0,
      duration:400, 
      timingFunction:'ease'
    })
    ani.opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(1).step();
    this.setData({
        ani2:ani.export()
    })
    ani=wx.createAnimation({
      delay: 0,
      duration:400, 
      timingFunction:'ease'
    })
    ani.opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(0.5).step().opacity(0.8).step().opacity(0.2).step().opacity(1).step();
    this.setData({
        ani3:ani.export()
    })
        var page = this.data.landList.length
        this.getData(6,page)
        this.setData({
            bottom:false
        })
  },

    onClose1(){
      this.setData({
          rightshow:!this.data.rightshow
      })
    },
    onClose2(){
        this.setData({
        tabSelect:!this.data.tabSelect
        })
    },
    reright(){
        this.setData({
            rightsed1:-1,
            rightsed2:-1,
            rightshow:false,
            landList:[]
        })
        
        this.getData(6,0);
    },
    refreash(){
        this.setData({
            areaArray:'',
            landList:[]
        })
    this.getData(6,0);
    },

    onPullDownRefresh:function () {
        wx.showLoading({
          title: '刷新中',
        })
     setTimeout(()=>{ wx.stopPullDownRefresh({
     success: (res) => {}})
     wx.hideLoading({
       success: (res) => {},
     })},1000)  
 },

  compare : function (prop,index) {
    return function (obj1, obj2) {
        var val1 = obj1[prop];
        var val2 = obj2[prop];
        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
            val1 = Number(val1);
            val2 = Number(val2);
        }

        if(index==0){
            return val1-val2;
        }else{
            return val2-val1;
        }
            
    } 
}
})